package cn.springmvc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.springmvc.dao.BaseDAO;
import cn.springmvc.model.ResultBaseBean;
import cn.springmvc.model.entity.PhotoBean;
import cn.springmvc.service.IPhotoService;

@Service
public class PhotoServiceImpl implements IPhotoService {
	private static final Logger logger = Logger.getLogger(PhotoServiceImpl.class);
	
	@Autowired
	@Qualifier("photoDAOImpl")
	private BaseDAO<PhotoBean> photoDAO;
	
	ResultBaseBean<PhotoBean> resultBaseBean;


	public ResultBaseBean<PhotoBean> insert(PhotoBean t) {
		resultBaseBean = new ResultBaseBean<PhotoBean>();
		logger.info("Prepare to create photo with name: " + t.getPhotoName());

		boolean result = photoDAO.insert(t);
		
		if(result){
			resultBaseBean.setSuccess(true);
			resultBaseBean.setMessage("添加照片"+t.getPhotoName()+"成功");
			logger.info("添加照片"+t.getPhotoName()+"成功");
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("添加照片失败");
			logger.info("添加照片"+t.getPhotoName()+"失败");
		}

		return resultBaseBean;
	}

	public ResultBaseBean<PhotoBean> select(PhotoBean t) {
		resultBaseBean = new ResultBaseBean<PhotoBean>();
		List<PhotoBean> PhotoBeanList = photoDAO.select(t);
		if(PhotoBeanList != null){
			resultBaseBean.setResultList(PhotoBeanList);
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("获取照片列表失败");
		}
		
		return resultBaseBean;
	}

	public ResultBaseBean<PhotoBean> delete(PhotoBean t) {
		resultBaseBean = new ResultBaseBean<PhotoBean>();
		boolean result = photoDAO.delete(t);
		if(result){
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("删除照片失败");
		}
		return resultBaseBean;
	}

	public ResultBaseBean<PhotoBean> update(PhotoBean t) {
		resultBaseBean = new ResultBaseBean<PhotoBean>();
		boolean result = photoDAO.update(t);
		if(result){
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("更新照片失败");
		}
		return resultBaseBean;
	}

	public ResultBaseBean<PhotoBean> selectOne(PhotoBean t) {
		resultBaseBean = new ResultBaseBean<PhotoBean>();
		PhotoBean PhotoBean = photoDAO.selectOne(t);
		if(PhotoBean != null){
			List<PhotoBean> PhotoBeanList = new ArrayList<PhotoBean>();
			PhotoBeanList.add(PhotoBean);
			resultBaseBean.setResultList(PhotoBeanList);
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("获取照片失败");
		}
		
		return resultBaseBean;
	}
	
	public ResultBaseBean<PhotoBean> getResultBaseBean() {
		return resultBaseBean;
	}

	public void setResultBaseBean(ResultBaseBean<PhotoBean> resultBaseBean) {
		this.resultBaseBean = resultBaseBean;
	}

}
